package com.xyzq.webapp.action.ya.data;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.xyzq.common.csv.AnalyseCSV;
import com.xyzq.common.csv.model.AccountStock;
import com.xyzq.model.common.DataDictionary;
import com.xyzq.model.ya.data.Account;
import com.xyzq.model.ya.data.AccountDayFund;
import com.xyzq.model.ya.data.AccountDayMoney;
import com.xyzq.model.ya.data.AccountDayStock;
import com.xyzq.model.ya.data.AccountMonthAssert;
import com.xyzq.model.ya.data.AccountMonthProfit;
import com.xyzq.model.ya.data.AccountMonthReward;
import com.xyzq.model.ya.data.AccountSpecial;
import com.xyzq.model.ya.data.Staff;
import com.xyzq.model.ya.data.StaffMonthReport;
import com.xyzq.model.ya.data.StaffSellFund;
import com.xyzq.model.ya.data.SubAccount;
import com.xyzq.service.common.DataDictionaryManager;
import com.xyzq.service.ya.AccountDayFundManager;
import com.xyzq.service.ya.AccountDayMoneyManager;
import com.xyzq.service.ya.AccountDayStockManager;
import com.xyzq.service.ya.AccountManager;
import com.xyzq.service.ya.AccountMonthAssertManager;
import com.xyzq.service.ya.AccountMonthProfitManager;
import com.xyzq.service.ya.AccountMonthRewardManager;
import com.xyzq.service.ya.AccountSpecialManager;
import com.xyzq.service.ya.StaffManager;
import com.xyzq.service.ya.StaffMonthReportManager;
import com.xyzq.service.ya.StaffSellFundManager;
import com.xyzq.service.ya.SubAccountManager;
import com.xyzq.webapp.action.BaseAction;

public class DataUploadAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4784279239833997525L;

	private AccountDayFundManager accountDayFundManager;
	private AccountDayMoneyManager accountDayMoneyManager;
	private AccountDayStockManager accountDayStockManager;
	private AccountManager accountManager;
	private AccountMonthAssertManager accountMonthAssertManager;
	private AccountMonthProfitManager accountMonthProfitManager;
	private AccountMonthRewardManager accountMonthRewardManager;
	private AccountSpecialManager accountSpecialManager;
	private SubAccountManager subAccountManager;
	private StaffManager staffManager;
	private StaffMonthReportManager staffMonthReportManager;
	private StaffSellFundManager staffSellFundManager;
	private DataDictionaryManager dataDictionaryManager;

	private File file;
	private List<DataDictionary> fileTypes;
	private String fileType;
	private Date day;
	private String month;
	private String result;

	private AnalyseCSV analyseCSV;

	@Override
	public String execute() {
		fileTypes = this.dataDictionaryManager
				.findByModule("dataUploadFileTypes");
		return INPUT;
	}

	public String upload() throws Exception {

		if (fileType.equals("客户每日资金")) {
			result = uploadAccountDayMoney(this.analyseCSV.analyse(fileType,
					file, new AccountDayMoney()));
		}
		if (fileType.equals("客户每日基金")) {
			result = this.uploadAccountDayFund(this.analyseCSV.analyse(
					fileType, file, new AccountDayFund()));
		}
		if (fileType.equals("客户每日证券")) {

			result = this.uploadAccountDayStock(this.analyseCSV.analyse(
					fileType, file, new AccountDayStock()));

		}
		if (fileType.equals("客户月度利润")) {

			result = this.uploadAccountMonthProfit(this.analyseCSV.analyse(
					fileType, file, new AccountMonthProfit()));

		}
		if (fileType.equals("客户月度资产报告")) {

			result = this.uploadAccountMonthAssert(this.analyseCSV.analyse(
					fileType, file, new AccountMonthAssert()));

		}
		if (fileType.equals("客户月度奖励")) {

			result = this.uploadAccountMonthReward(this.analyseCSV.analyse(
					fileType, file, new AccountMonthReward()));

		}
		if (fileType.equals("特别客户")) {

			result = this.uploadAccountSpecial(this.analyseCSV.analyse(
					fileType, file, new AccountSpecial()));

		}
		if (fileType.equals("员工月销售基金")) {

			result = this.uploadStaffSellFund(this.analyseCSV.analyse(fileType,
					file, new StaffSellFund()));

		}
		if (fileType.equals("员工月度报告")) {

			result = this.uploadStaffMonthReport(this.analyseCSV.analyse(
					fileType, file, new StaffMonthReport()));

		}
		if (fileType.equals("客户帐户数据")) {

			result = this.uploadAccount(this.analyseCSV.analyse(fileType, file,
					new Account()));

		}
		if (fileType.equals("客户帐户关联数据")) {

			result = this.uploadAccountStock(this.analyseCSV.analyse(fileType,
					file, new AccountStock()));

		}
		return SUCCESS;
	}

	private String uploadAccountStock(ArrayList<Object> list) {
		for (Object object : list) {
			AccountStock as = (AccountStock) object;
			Account account = this.accountManager.findByAccountId(as
					.getAccount());
			Boolean save = false;
			if (account != null) {
				if (as.getMarket().equals("上海")
						&& !account.getStockShNumber().equals(
								as.getStockNumber())) {
					account.setStockShNumber(as.getStockNumber());
					account.setStockShOpenDay(as.getOpenDay());
					save = true;
				} else if (as.getMarket().equals("深圳")
						&& !account.getStockSzNumber().equals(
								as.getStockNumber())) {
					account.setStockSzNumber(as.getStockNumber());
					account.setStockSzOpenDay(as.getOpenDay());
					save = true;
				} else if (as.getMarket().equals("沪B")
						&& !account.getStockShNumberB().equals(
								as.getStockNumber())) {
					account.setStockShNumberB(as.getStockNumber());
					account.setStockShOpenDayB(as.getOpenDay());
					save = true;
				} else if (as.getMarket().equals("深B")
						&& !account.getStockSzNumberB().equals(
								as.getStockNumber())) {
					account.setStockSzNumberB(as.getStockNumber());
					account.setStockSzOpenDayB(as.getOpenDay());
					save = true;
				}
				if (save) {
					account.setLastModifyTime(new Date());
					this.accountManager.save(account);
				}
			}
		}
		return "upload to Account table!";
	}

	private String uploadAccount(ArrayList<Object> list) {
		for (Object object : list) {
			Account account = (Account) object;
			Staff staff = this.staffManager.findByStaffId(account.getStaff()
					.getStaffId());
			account.setStaff(staff);
			account.setSubTime(new Date());
			this.accountManager.save(account);
		}
		return "upload to Account table!";
	}

	private String uploadStaffMonthReport(ArrayList<Object> list) {
		for (Object object : list) {
			StaffMonthReport smr = (StaffMonthReport) object;
			Staff staff = this.staffManager.findByStaffId(smr.getStaff()
					.getStaffId());
			smr.setStaff(staff);
			smr.setMonth(getMonth());
			smr.setSubTime(new Date());
			this.staffMonthReportManager.save(smr);
		}
		return "upload to Staff Month Report table!";
	}

	private String uploadStaffSellFund(ArrayList<Object> list) {
		for (Object object : list) {
			StaffSellFund ssf = (StaffSellFund) object;
			Staff staff = this.staffManager.findByStaffId(ssf.getStaff()
					.getStaffId());
			ssf.setStaff(staff);
			ssf.setMonth(getMonth());
			ssf.setSubTime(new Date());
			this.staffSellFundManager.save(ssf);
		}
		return "upload to Staff Sell Fund table!";
	}

	private String uploadAccountSpecial(ArrayList<Object> list) {
		for (Object object : list) {
			AccountSpecial as = (AccountSpecial) object;
			Account account = this.accountManager.findByAccountId(as
					.getAccount().getAccount());
			as.setAccount(account);
			as.setSubTime(new Date());
			this.accountSpecialManager.save(as);
		}
		return "upload to Account Special table!";
	}

	private String uploadAccountMonthReward(ArrayList<Object> list) {
		for (Object object : list) {
			AccountMonthReward amr = (AccountMonthReward) object;
			Account account = this.accountManager.findByAccountId(amr
					.getAccount().getAccount());
			amr.setAccount(account);
			amr.setMonth(getMonth());
			amr.setSubTime(new Date());
			this.accountMonthRewardManager.save(amr);
		}
		return "upload to Account Month Reward table!";
	}

	private String uploadAccountMonthAssert(ArrayList<Object> list) {
		for (Object object : list) {
			AccountMonthAssert ama = (AccountMonthAssert) object;
			Account account = this.accountManager.findByAccountId(ama
					.getAccount().getAccount());
			ama.setAccount(account);
			ama.setMonth(getMonth());
			ama.setSubTime(new Date());
			this.accountMonthAssertManager.save(ama);
		}
		return "upload to Account Month Assert table!";
	}

	private String uploadAccountMonthProfit(ArrayList<Object> list) {
		for (Object object : list) {
			AccountMonthProfit amp = (AccountMonthProfit) object;
			Account account = this.accountManager.findByAccountId(amp
					.getAccount().getAccount());
			amp.setAccount(account);
			amp.setMonth(getMonth());
			amp.setSubTime(new Date());
			this.accountMonthProfitManager.save(amp);
		}
		return "upload to Account Month Profit table!";
	}

	private String uploadAccountDayStock(ArrayList<Object> list) {
		for (Object object : list) {
			AccountDayStock ads = (AccountDayStock) object;
			Account account = this.accountManager.findByAccountId(ads
					.getAccount().getAccount());
			if (account == null) {
				SubAccount subAccount = this.subAccountManager
						.findBySubAccountId(ads.getAccount().getAccount());
				if (subAccount.getType().equals("信用")) {
					ads.setType("信用");
				}
				account = subAccount.getAccount();
			}
			ads.setAccount(account);
			ads.setDay(day);
			ads.setSubTime(new Date());
			this.accountDayStockManager.save(ads);
		}
		return "upload to Account Day Stock table!";
	}

	private String uploadAccountDayFund(ArrayList<Object> list)
			throws Exception {
		for (Object object : list) {
			AccountDayFund accountDayFund = (AccountDayFund) object;
			Account account = this.accountManager
					.findByAccountId(accountDayFund.getAccount().getAccount());
			if (account == null) {
				SubAccount subAccount = this.subAccountManager
						.findBySubAccountId(accountDayFund.getAccount()
								.getAccount());
				account = subAccount.getAccount();
			}
			accountDayFund.setAccount(account);
			accountDayFund.setDay(day);
			accountDayFund.setSubTime(new Date());
			this.accountDayFundManager.save(accountDayFund);
		}
		return "upload to Account Day Fund table!";
	}

	private String uploadAccountDayMoney(ArrayList<Object> list)
			throws Exception {
		for (Object object : list) {
			AccountDayMoney accountDayMoney = (AccountDayMoney) object;
			Account account = this.accountManager
					.findByAccountId(accountDayMoney.getAccount().getAccount());
			if (account == null) {
				SubAccount subAccount = this.subAccountManager
						.findBySubAccountId(accountDayMoney.getAccount()
								.getAccount());
				if (subAccount.getType().equals("多银行")) {
					accountDayMoney.setType("多银行");
				} else if (subAccount.getType().equals("信用")) {
					accountDayMoney.setType("信用");
				}
				account = subAccount.getAccount();
			}
			accountDayMoney.setAccount(account);
			accountDayMoney.setDay(day);
			accountDayMoney.setSubTime(new Date());
			this.accountDayMoneyManager.save(accountDayMoney);
		}
		return "upload to Account Day Money table!";
	}

	public AccountDayFundManager getAccountDayFundManager() {
		return accountDayFundManager;
	}

	public AccountDayMoneyManager getAccountDayMoneyManager() {
		return accountDayMoneyManager;
	}

	public AccountDayStockManager getAccountDayStockManager() {
		return accountDayStockManager;
	}

	public AccountManager getAccountManager() {
		return accountManager;
	}

	public AccountMonthAssertManager getAccountMonthAssertManager() {
		return accountMonthAssertManager;
	}

	public AccountMonthProfitManager getAccountMonthProfitManager() {
		return accountMonthProfitManager;
	}

	public AccountMonthRewardManager getAccountMonthRewardManager() {
		return accountMonthRewardManager;
	}

	public AccountSpecialManager getAccountSpecialManager() {
		return accountSpecialManager;
	}

	public StaffManager getStaffManager() {
		return staffManager;
	}

	public StaffMonthReportManager getStaffMonthReportManager() {
		return staffMonthReportManager;
	}

	public StaffSellFundManager getStaffSellFundManager() {
		return staffSellFundManager;
	}

	public DataDictionaryManager getDataDictionaryManager() {
		return dataDictionaryManager;
	}

	public File getFile() {
		return file;
	}

	public List<DataDictionary> getFileTypes() {
		return fileTypes;
	}

	public String getFileType() {
		return fileType;
	}

	public Date getDay() {
		return day;
	}

	public void setAccountDayFundManager(
			AccountDayFundManager accountDayFundManager) {
		this.accountDayFundManager = accountDayFundManager;
	}

	public void setAccountDayMoneyManager(
			AccountDayMoneyManager accountDayMoneyManager) {
		this.accountDayMoneyManager = accountDayMoneyManager;
	}

	public void setAccountDayStockManager(
			AccountDayStockManager accountDayStockManager) {
		this.accountDayStockManager = accountDayStockManager;
	}

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public void setAccountMonthAssertManager(
			AccountMonthAssertManager accountMonthAssertManager) {
		this.accountMonthAssertManager = accountMonthAssertManager;
	}

	public void setAccountMonthProfitManager(
			AccountMonthProfitManager accountMonthProfitManager) {
		this.accountMonthProfitManager = accountMonthProfitManager;
	}

	public void setAccountMonthRewardManager(
			AccountMonthRewardManager accountMonthRewardManager) {
		this.accountMonthRewardManager = accountMonthRewardManager;
	}

	public void setAccountSpecialManager(
			AccountSpecialManager accountSpecialManager) {
		this.accountSpecialManager = accountSpecialManager;
	}

	public void setStaffManager(StaffManager staffManager) {
		this.staffManager = staffManager;
	}

	public void setStaffMonthReportManager(
			StaffMonthReportManager staffMonthReportManager) {
		this.staffMonthReportManager = staffMonthReportManager;
	}

	public void setStaffSellFundManager(
			StaffSellFundManager staffSellFundManager) {
		this.staffSellFundManager = staffSellFundManager;
	}

	public void setDataDictionaryManager(
			DataDictionaryManager dataDictionaryManager) {
		this.dataDictionaryManager = dataDictionaryManager;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public void setFileTypes(List<DataDictionary> fileTypes) {
		this.fileTypes = fileTypes;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}

	public void setDay(Date day) {
		this.day = day;
	}

	public void setAnalyseCSV(AnalyseCSV analyseCSV) {
		this.analyseCSV = analyseCSV;
	}

	public AnalyseCSV getAnalyseCSV() {
		return analyseCSV;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public String getMonth() {
		return month;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getResult() {
		return result;
	}

	public void setSubAccountManager(SubAccountManager subAccountManager) {
		this.subAccountManager = subAccountManager;
	}

	public SubAccountManager getSubAccountManager() {
		return subAccountManager;
	}

}
